function A(){
    return new Promise((resolve,reject)=>{
        setTimeout(()=>{
            console.log('A')
            reject('a is no')
        },1500)
        
    })
   

}

function B(){
    return new Promise((resolve,reject)=>{
        setTimeout(()=>{
            console.log('B')
            reject('b is ok')
        },1000)
        
    })
    
}

function C(){

   setTimeout(()=>{
        console.log('C')
    },300)
}

// A().then(()=>{
//     B().then(()=>{
//         C() 
//     })
// })


// Promise.all([A(),B()])
// .then((res)=>{
//     console.log(res)
//     C()
// })


Promise.myAll = function(promises){
    if(!Array.isArray(promises)){
        throw new TypeError('promises must be an array')
    }
    return new Promise((resolve,reject)=>{
      let count = 0
      let result = []
      for(let i=0;i<promises.length;i++){
        let item = promises[i]
        item.then((res)=>{
          result[i] = res
          count++
          if(count === promises.length){
            resolve(result) 
           }  
       }).catch((err)=>{
        reject(err)
        return
       }) 
      }        
    }) 
}

// Promise.myAll([A(),B()])
// .then((res)=>{
//     console.log(res)
//     C()
// })

// Promise.race([A(),B()]).then((res)=>{
//     console.log(res)
//     C()
// })

Promise.myRace = function(promises){
    if(!Array.isArray(promises)){
        throw new TypeError('promises must be an array')
    } 
    return new Promise((resolve,reject)=>{
        for(item of promises){
            item.then((res)=>{
                resolve(res)
            }).catch((err)=>{
                reject(err)
            })
        }
    })
}

// Promise.myRace([A(),B()]).then((res)=>{
//     console.log(res)
//     C()
// })

Promise.any([A(),B()]).then((res)=>{
    console.log(res)
    C() 
}).catch((err)=>{
    console.log(err)
})

Promise.myAny = function(promises){
    if(!Array.isArray(promises)){
        throw new TypeError('promises must be an array')
    }
    
    return new Promise((resolve,reject)=>{
        let count = 0
        let result = []
        for(let i=0;i<promises.length;i++){
            let item = promises[i]
            item.then((res)=>{
                resolve(res) 
            }).catch((err)=>{
                result[i] = err
                count++
                if(count === promises.length){
                    reject(result)
                }
            })
        } 
    })
}

// Promise.myAny([A(),B()]).then((res)=>{
//     console.log(res)
//     C() 
// })

Promise.allSettled([A(),B()]).then((res)=>{
    console.log(res)
    C()
})

Promise.myAllSettled = function(promises){
    if(!Array.isArray(promises)){
        throw new TypeError('promises must be an array')
     }
     return new Promise((resolve,reject)=>{
        let count = 0
        let result = []
        let error = []
       for(let i = 0;i<promises.length;i++){
        let item = promises[i]
        item.then((res)=>{
           count++
           result[i] = {
            status:'fulfilled',
            value:res
           }
        }),
        (err)=>{
            count++
            result[i] = {
                status:'rejected',
                value:err
            }
        } 
       } 
       if(count === promises.length){
        resolve(result) 
       }
     }) 
}

Promise.prototype.finally = function(callback){
  this.then(
    (res)=>{
    callback()
    
  },
  (err)=>{
    callback()

  }) 

 
}